package com.intelligentpension.kotlinbluetooth

import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import com.hjq.toast.ToastUtils
import com.intelligentpension.kotlinbluetooth.ble.BleCustomerActivity
import com.intelligentpension.kotlinbluetooth.ble.BleServerActivity
import com.intelligentpension.kotlinbluetooth.ble.LowPowerCustomerActivity
import com.intelligentpension.kotlinbluetooth.ble.LowPowerServerActivity
import com.permissionx.guolindev.PermissionX

class MainActivity : AppCompatActivity(), View.OnClickListener {
    private lateinit var mCustomer: Button
    private lateinit var mService: Button
    private lateinit var mBleCustomer: Button
    private lateinit var mBleService: Button

    @RequiresApi(Build.VERSION_CODES.S)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mCustomer = findViewById<Button>(R.id.customer)
        mService = findViewById<Button>(R.id.service)

        mBleCustomer = findViewById<Button>(R.id.ble_customer)
        mBleService = findViewById<Button>(R.id.ble_service)

        mCustomer.setOnClickListener(this@MainActivity)
        mService.setOnClickListener(this@MainActivity)
        mBleCustomer.setOnClickListener(this@MainActivity)
        mBleService.setOnClickListener(this@MainActivity)



        PermissionX.init(this@MainActivity)
            .permissions(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.BLUETOOTH_ADVERTISE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.BLUETOOTH_SCAN,
                Manifest.permission.BLUETOOTH_CONNECT,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.BLUETOOTH_ADMIN
            )
            .onForwardToSettings { scope, deniedList ->
                scope.showForwardToSettingsDialog(
                    deniedList,
                    "You need to allow necessary permissions in Settings manually",
                    "OK",
                    "Cancel"
                )
            }.request { allGranted, grantedList, deniedList ->
                if (allGranted) {
                    Toast.makeText(this, "All permissions are granted", Toast.LENGTH_LONG).show()
                } else {
                    Toast.makeText( this,"These permissions are denied: $deniedList", Toast.LENGTH_LONG).show()
                }
            }

        dealBlooth()
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.customer -> {
                Intent(this@MainActivity, BleCustomerActivity::class.java).apply {
                    startActivity(this)
                }
            }
            R.id.service -> {
                Intent(this@MainActivity, BleServerActivity::class.java).apply {
                    startActivity(this)
                }
            }
            R.id.ble_customer -> {
                Intent(this@MainActivity, LowPowerCustomerActivity::class.java).apply {
                    startActivity(this)
                }

            }
            R.id.ble_service -> {
                Intent(this@MainActivity, LowPowerServerActivity::class.java).apply {
                    startActivity(this)
                }

            }
        }
    }

    private fun dealBlooth() {
        val mAdapter: BluetoothAdapter =
            (getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager).adapter

        //当前是否支持蓝牙
        if (mAdapter == null) {
            ToastUtils.show("本机没有找到蓝牙硬件或驱动")
            finish()
        } else {
            //如果当前设备的蓝牙没开启
            if (!mAdapter.isEnabled) {
                //只有sdk大于30的时候,才会需要这个权限
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                        ToastUtils.show("当前没有权限Manifest.permission.BLUETOOTH_CONNECT")
                        return
                    }
                }
                mAdapter.enable()
            }
            if (!packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
                ToastUtils.show("本机不支持低能耗蓝牙")
                finish()
                return
            }
        }

    }
}